package com.sharemarking.application.common.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sharemarking.application.common.BaseSpecification;
import com.sharemarking.application.common.entity.Company;
import com.sharemarking.application.common.entity.Department;
import com.sharemarking.application.common.entity.Employee;
import com.sharemarking.application.common.repository.CompanyRepo;
import com.sharemarking.application.common.repository.DepartmentRepo;
import com.sharemarking.application.common.repository.EmployeeRepo;

@Controller
@RequestMapping("department")
public class DepartmentController {
	
	@Autowired
	private DepartmentRepo dr;
	
	@Autowired
	private EmployeeRepo er;
	
	@Autowired
	private CompanyRepo cr;
	
	@RequestMapping("getAllDepartment")
	@ResponseBody
	public List<Department> getAllDepartment(){
		List<Department> list=null;
		list=(List<Department>) dr.findAll();
		return list;
	}
	
	
	/**
	 * 部门数据列表
	 * @param e
	 * @param page
	 * @param size
	 * @param model
	 */
	@GetMapping(value="index")
	@PreAuthorize("hasPermission('sys:department',1)")
	public void index(Department e,Integer page,Integer size,Model model){
		
		if(page==null){
			page=0;
		}else{
			page -=1;
		}
		if(size==null){
			size=10;
		}
		
		Sort sort=new Sort(Direction.DESC,"id");
		PageRequest req=new PageRequest(page, size, sort);
		Page<Department> p=dr.findAll(new BaseSpecification<Department>(e),req);
		
		model.addAttribute("page",p);
		model.addAttribute("search",e);
	}
	
	/**
	 * 获取实体借口
	 * @param obj
	 * @param model
	 * @return
	 */
	@GetMapping(value="{id}")
	@PreAuthorize("hasPermission('sys:department',1)")
	public String get(Department obj,Model model){
		
		if(obj.getId()!=0)
			obj=dr.findOne(obj.getId());
		model.addAttribute("obj",obj);
		
		return "department/form";
	}
	
	/**
	 * id==0  =>  新增实体
	 * id!=0  =>  更新实体
	 * @param e
	 * @return
	 */
	@PostMapping(value="{id}")
	@Transactional
	@PreAuthorize("hasPermission('sys:department',#target.id==0?8:4)")
	public String post(Department target,Model model,HttpServletRequest request){
		
		if(target.getId()==0){
			//关联表
//			Integer employeeId=Integer.parseInt(request.getParameter("employeeId"));
			Integer companyId=Integer.parseInt(request.getParameter("companyId"));

//			Employee e=er.findOne(employeeId);
			Company c=cr.findOne(companyId);
			
//			target.setEmployee1(e);
			target.setCompany(c);
		
			dr.save(target);
			return "redirect:index";
		}else {
			
			Department department=dr.findOne(target.getId());
			if(department==null)
				return null;
			
			//编辑关联表
//			Integer employeeId=Integer.parseInt(request.getParameter("employeeId"));
			Integer companyId=Integer.parseInt(request.getParameter("companyId"));

//			Employee e=er.findOne(employeeId);
			Company c=cr.findOne(companyId);
			
//			department.setEmployee1(e);
			department.setCompany(c);
			
			department.setName(target.getName());
			department.setManager(target.getManager());
			department.setDescriber(target.getDescriber());
		
		}
		return "redirect:index";
	}
	
	//删除部门
	@GetMapping(value="{id}/delete")
	@Transactional
	//@PreAuthorize("hasPermission('sys:department',16)")
	public String get(@PathVariable("id") Integer id){
		dr.findOne(id).setDelFlag(true);
		return "redirect:../index";
		
	}
	
	//ajax验证部门的唯一性
	@PostMapping(value="checkName")
	@ResponseBody
	 public String findByName(String param){
		 return dr.findByName(param)!=null?"该部门已存在了":"y";
	 }
	  
}
